﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lant.Net.IGame;

namespace Lant.Net.Game
{
	public partial class Snake : UserControl, Lant.Net.IGame.IGame
	{
		private const string _GameName = "贪吃蛇 v1.0";
		private const string _GameRule = "抛开传统的按键模式，点击屏幕，自动判断蛇头的方向，游戏开发中，点击中间的按钮做测试";
		private readonly int _SnakeOffset = 5;
		private Timer timer = null;
		private SnakeWay snakeWay = SnakeWay.UNKONW;
		public Snake()
		{
			InitializeComponent();
			btnTest = new Button();
			btnTest.Left = 100;
			btnTest.Top = 100;
			btnTest.Width = 20;
			btnTest.Text = "A";
			btnTest.Click += new EventHandler(btnTest_Click);

			timer = new Timer();
			timer.Enabled = false;
			timer.Tick += new EventHandler(timer_Tick);
			MessageBox.Show(_GameRule, "提示");
		}

		#region 实现IGame接口
		public string GameName { get { return _GameName; } }
		public string GameRule { get { return _GameRule; } }
		private Button btnTest;

		public bool Start()
		{
			this.Controls.Add(btnTest);
			snakeWay = SnakeWay.LEFT;
			btnTest.Text = "左";
			return true;
		}

		public object Control()
		{
			return this;
		}

		#endregion

		#region 事件

		void btnTest_Click(object sender, EventArgs e)
		{
			timer.Interval = 100;
			timer.Enabled = true;
		}
		void timer_Tick(object sender, EventArgs e)
		{
			if (snakeWay == SnakeWay.LEFT && this.btnTest.Left >= _SnakeOffset)
				this.btnTest.Left -= _SnakeOffset;
			else if (snakeWay == SnakeWay.RIGHT && this.btnTest.Width + this.btnTest.Left <= this.Width)
				this.btnTest.Left += _SnakeOffset;
			else if (snakeWay == SnakeWay.UP && this.btnTest.Top >= _SnakeOffset)
				this.btnTest.Top -= _SnakeOffset;
			else if (snakeWay == SnakeWay.DOWN && this.btnTest.Height + this.btnTest.Top <= this.Height)
				this.btnTest.Top += _SnakeOffset;
			else
			{
				this.timer.Enabled = false;
				MessageBox.Show("超出边界或方向未知", "提示");
			}
		}

		private void Snake_Click(object sender, EventArgs e)
		{
			
		}

		private void Snake_MouseDown(object sender, MouseEventArgs e)
		{
			int x = e.X;
			int y = e.Y;

			bool compare = Math.Abs(x - this.btnTest.Left) > Math.Abs(y - this.btnTest.Top);

			if (x < this.btnTest.Left && compare)
			{
				//左边
				this.btnTest.Text = "左";
				this.snakeWay = SnakeWay.LEFT;
			}
			else if (x > this.btnTest.Left && compare)
			{
				//右边
				this.btnTest.Text = "右";
				this.snakeWay = SnakeWay.RIGHT;
			}
			else if (y < this.btnTest.Top && !compare)
			{ 
				//上边
				this.btnTest.Text = "上";
				this.snakeWay = SnakeWay.UP;
			}
			else if (y > this.btnTest.Top && !compare)
			{ 
				//下
				this.btnTest.Text = "下";
				this.snakeWay = SnakeWay.DOWN;
			}
			else
			{ 
				//未知
				this.btnTest.Text = "未知";
				this.snakeWay = SnakeWay.UNKONW;
			}
		}
		#endregion
	}
	public class SnakeInfo
	{ 

	}
	/// <summary>
	/// 蛇运动的方向
	/// </summary>
	public enum SnakeWay
	{
		UNKONW = 0,
		UP = 1,
		RIGHT = 2,
		DOWN = 3,
		LEFT = 4
	}
}
